package oylj.recycle.all_in_one.card;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.huashi.otg.sdk.HSIDCardInfo;
import com.huashi.otg.sdk.HandlerMsg;
import com.huashi.otg.sdk.HsSerialPortSDK;

import java.util.Objects;

public class CardLib {
    HsSerialPortSDK ComApi = null;
    private boolean isInitialized = false;
    private static final String TAG = "CardLib";
    public CardLib(Context context) throws Exception {
        String filePath = Objects.requireNonNull(context.getExternalFilesDir(null)).getAbsolutePath();
        ComApi = new HsSerialPortSDK(context, filePath);
        init();
    }
    public void init(){
        String Port = "/dev/ttyS9";
        int baud = 115200;
        int ret = ComApi.init(Port, baud, 0);
        if (ret == 0) {
            String tmp = ComApi.GetSAM();
            if(!tmp.isEmpty()) {
                isInitialized = true;
                Log.d(TAG, "init: 一体式读卡器初始化成功");
            }
        } else {
            Log.e(TAG, "连接失败，未找到串口或者无打开串口权限，返回值：" + ret);
        }
    }

    public boolean isInitialized() {
        return isInitialized;
    }
    @JavascriptInterface
    public String readIdCard(){
        if (!isInitialized) {
            Log.d(TAG, "readIdCard: 请先连接读卡器");
            return "";
        }

        if (ComApi.Authenticate(1000) != 0) {
            Log.d(TAG, "readIdCard: 卡认证失败");
            return "";
        }
        HSIDCardInfo ici = new HSIDCardInfo();

        if (ComApi.Read_CardFP(ici, 2300) == 0) {
            String id = ici.getIDCard();
            Log.d(TAG, "readIdCard: "+id);
            return id;
        } else {
            Log.d(TAG, "readIdCard: 读卡失败");
            return "";
        }
    }
    @JavascriptInterface
    public String readIcCard(){
        if (!isInitialized) {
            Log.d(TAG, "readIdCard: 请先连接读卡器");
            return "";
        }

        byte nBlock = 0;
        byte nMode = 0;
        String str2;
        String KEY = "FF,FF,FF,FF,FF,FF";

        byte[] pSNR = new byte[]{(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00};
        byte[] KeyBuff = new byte[6];
        byte[] ReadBuff = new byte[16];
        if (String2byte(KeyBuff, KEY) == -1) {
            Log.d(TAG, "秘钥输入不正确 ");
            return "";
        }

        int Eor = ComApi
                .M1_Read(nMode, pSNR, nBlock, KeyBuff, ReadBuff);
        if (Eor == 0) {
            str2 = "卡序列号:";
            for (int i = 0; i < pSNR.length; i++) {
                str2 = str2 + String.format("%02x ", pSNR[3 - i]);
            }
            str2 = str2 + "\n" + "块数据:";
            for (int i = 0; i < ReadBuff.length; i++) {
                str2 = str2 + String.format("%02x ", ReadBuff[i]);
            }
            Log.d(TAG, "readIcCard: "+str2);
            return str2;
        } else {
            Log.d(TAG, "读取失败 "+Eor);
            return "";
        }
    }

    //将逗号分隔的字符串转换为byte数组
    public int String2byte(byte[] b, String StrBuf) {
        String[] parts = StrBuf.split(",");
        int Itmp;
        int Len = parts.length;
        if (Len >= b.length) {
            for (int i = 0; i < b.length; i++) {
                try {
                    Itmp = Integer.valueOf(parts[i], 16);
                    b[i] = (byte) Itmp;
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
            return Len;
        }
        return -1;
    }
}
